class Vehicle { typeName!: string }
class Car extends Vehicle { carNo!: number }

// 使用类型断言的好处，instanceof有if的嵌套
const vehicle: Vehicle = new Car();
const car = vehicle as Car; // vehicle 实际是 Car
car.carNo = 1001;

// 使用类型断言的好处，规避类型检查
let name: string = 'age'
let symid = Symbol("userId")
let obj = { [symid]: 'symbol', name: "hello" }
console.log(obj[name as any])

// 强制断言对象类型
let rnb: string = 'name'
let money = { rnb: "hello" }
console.log((money as { [key: string]: any })[rnb])  // 不报错

// 类型声明
// let rnb: string = 'name'
// let money: { [key: string]: any; rnb: string } = { rnb: "hello" }
// console.log(money[rnb])  // 不报错

// 使用const 或者 as const
// const rnb: string = 'name'
// let rnb: string = 'name' as const


// 类型转换
class Vehicle2 { typeName!: string }
class Car2 extends Vehicle2 { carNo!: number }

// 使用类型断言的好处，instanceof有if的嵌套
const vehicle2: Vehicle2 = new Car2();
// const car = vehicle as Car; // vehicle 实际是 Car
const car2 = <Car2>vehicle2
car2.carNo = 1001;

// 当对象属性名为混合类型时，且包含symbol时
// 属性名类型定义为：string 时，属性名类型变为 string | number | symbol，symbol属性值类型为 any
type Record_ = { [key: string]: string }
let obj1: Record_ = {
  name: "张三",
  [Symbol("hello")]: true,
  0: "hello",
}  // 不报错

// 属性名类型定义为：number 时，属性名类型变为 number | symbol，symbol属性值类型为 any
type Record2_ = { [key: number]: string }
let obj2: Record2_ = {
  // name: "张三",  // 报错
  [Symbol("hello")]: true,
  0: "hello",
}

// 属性名类型定义为：symbol 时，属性名类型变为 symbol
type Record3_ = { [key: symbol]: string }
let obj3: Record3_ = {
  // name: "张三",  // 报错
  [Symbol("hello")]: "hello",
  // 0: "hello",  // 报错
}
export {}